package com.yhp.servlet;

import com.yhp.bean.Users;
import com.yhp.service.UsersService;
import com.yhp.service.iml.UsersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/login")
public class loginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String loginName = req.getParameter("loginName");
        String password = req.getParameter("password");
        req.getSession().setAttribute("name",loginName);
        req.getSession().setAttribute("password",password);
        req.setCharacterEncoding("utf-8");
        UsersService usersService = new UsersServiceImpl();
        Users users = usersService.getUsers(loginName, password);
        req.getSession().setAttribute("u1",users);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (users==null){
            resp.setContentType("text/html;charset=utf-8");
            writer.println("<script>location.href='login.jsp';alert('用户名或密码不正确');</script>");
        }else {
            resp.sendRedirect("index.jsp");
        }
    }
}
